package com.auxgroup.hr.front.service.performance.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.auxgroup.hr.front.dao.domain.PsCEvaChartT2;
import com.auxgroup.hr.front.dao.domain.PsCEvaChartT3;
import com.auxgroup.hr.front.dao.domain.PsCEvaDocOVw;
import com.auxgroup.hr.front.dao.domain.PsCEvaResultVw;
import com.auxgroup.hr.front.dao.repository.PsCEvaChartT2Repository;
import com.auxgroup.hr.front.dao.repository.PsCEvaDocOVwRepository;
import com.auxgroup.hr.front.dao.repository.PsCEvaResultVwRepository;
import com.auxgroup.hr.front.dao.repository.PscEvaChartT3Repository;
import com.auxgroup.hr.front.service.performance.AssessResultService;

/** 
 * @author lvliuhao 
 * @version 创建时间：2018年8月24日 下午2:39:54 
 * 类说明 
 */
@Service("assessResultService")
public class AssessResultServiceImpl implements AssessResultService {
	
	@Autowired
	private PsCEvaDocOVwRepository psCEvaDocOVwRepository;
	
	@Autowired
	private PsCEvaResultVwRepository psCEvaResultVwRepository;
	
	@Autowired
	private PsCEvaChartT2Repository psCEvaChartT2Repository;
	
	@Autowired
	private PscEvaChartT3Repository pscEvaChartT3Repository;

	@Override
	public List<Object[]> getEvaDocs(String emplid, String evaType,String queryKey) {
		if(StringUtils.isNotBlank(queryKey)){
			queryKey = "%"+queryKey+"%";
			return psCEvaDocOVwRepository.getEvaDocsByEmplidAndEvaTypeAndDescr(emplid, evaType,queryKey);
		}
		return psCEvaDocOVwRepository.getEvaDocsByEmplidAndEvaType(emplid, evaType);
		
	}

	@Override
	public PsCEvaResultVw getEvaResult(String docid) {
		return psCEvaResultVwRepository.findOne(docid);
	}


	@Override
	public PsCEvaDocOVw getEvaDoc(String emplid, String evaType, String sheetid) {
		PsCEvaDocOVw query = new PsCEvaDocOVw();
		query.setcEvaluateType(evaType);
		query.setcEvaSheetId(sheetid);
		query.setEmplid(emplid);
		query.setEmplRcd(new BigDecimal(0));
		List<PsCEvaDocOVw> docs = psCEvaDocOVwRepository.findAll(Example.of(query),new Sort(Direction.DESC, "cEvaDocId"));
		if(docs==null||docs.isEmpty()){
			return null;
		}
		return docs.get(0);
	}

	@Override
	public List<PsCEvaChartT2> getChartT2s(String docid) {
		PsCEvaChartT2 query = new PsCEvaChartT2();
		query.setcEvaDocId(docid);
		return psCEvaChartT2Repository.findAll(Example.of(query),new Sort(Direction.ASC, "cEvaDimensionId"));
	}

	@Override
	public List<PsCEvaChartT3> getChartT3s(String docid) {
		PsCEvaChartT3 query = new PsCEvaChartT3();
		query.setcEvaDocId(docid);
		return pscEvaChartT3Repository.findAll(Example.of(query),new Sort(Direction.ASC, "cEvaDimensionId","cEvaQuestion"));
	}

}
 